#include "printwindow.h"
#include "ui_printwindow.h"
#include <QRunnable>
#include <QMutexLocker>
#include <QPrintPreviewWidget>
#include <QPainter>
#include "printpreview.h"

PrintWindow::PrintWindow(PioCore *core, QWidget *parent)
    :
    QMainWindow(parent),
    ui(new Ui::PrintWindow),
    m_engine(core)
{
    ui->setupUi(this);
    m_preview = new QPrintPreviewWidget(this);
    setCentralWidget(m_preview);
    ui->actionCropImages->setChecked(true);
    ui->optionsDock->setShown(false);
    ui->width->setText(QString("%1 cm").arg(m_engine.itemWidth()));
    ui->height->setText(QString("%1 cm").arg(m_engine.itemHeight()));
    QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
    QString def = QPrinterInfo::defaultPrinter().printerName();
    foreach(QPrinterInfo p, printers) {
        ui->printer->addItem(p.printerName());
        if(p.printerName() == def)
            ui->printer->setCurrentIndex(ui->printer->count() - 1);
    }

    connect(m_preview, SIGNAL(paintRequested(QPrinter*)), &m_engine, SLOT(print(QPrinter*)));
    connect(&m_engine, SIGNAL(changed()), m_preview, SLOT(updatePreview()), Qt::QueuedConnection);
    connect(&m_engine, SIGNAL(completed(bool)), ui->actionPrint, SLOT(setEnabled(bool)));
    connect(ui->actionPageSetup, SIGNAL(triggered(bool)), ui->optionsDock, SLOT(setShown(bool)));
}

PrintWindow::~PrintWindow()
{
    delete ui;
}

void PrintWindow::showEvent(QShowEvent * e)
{
    QMainWindow::showEvent(e);
    m_engine.enable(true);
}

void PrintWindow::hideEvent(QHideEvent * e)
{
    m_engine.enable(false);
    QMainWindow::hideEvent(e);
}

void PrintWindow::on_actionZoomOut_triggered()
{
    m_preview->zoomOut();
}

void PrintWindow::on_actionPrevPage_triggered()
{
    if(m_preview->currentPage() > 1)
        m_preview->setCurrentPage(m_preview->currentPage() - 1);
}

void PrintWindow::on_actionNextPage_triggered()
{
    if(m_preview->currentPage() < m_preview->pageCount())
        m_preview->setCurrentPage(m_preview->currentPage() + 1);
}

void PrintWindow::on_actionPageSetup_triggered()
{

}

void PrintWindow::on_actionZoomIn_triggered()
{
    m_preview->zoomIn();
}

void PrintWindow::on_actionRotateImagesToFit_triggered(bool checked)
{
    m_engine.setAutoRotate(checked);
}

void PrintWindow::on_actionLandscapeFormat_triggered(bool checked)
{
    if(checked)
        m_preview->setLandscapeOrientation();
    else
        m_preview->setPortraitOrientation();
}

void PrintWindow::on_actionCropImages_triggered()
{
    ui->actionCropImages->setChecked(true);
    ui->actionShrinkImages->setChecked(false);
    ui->actionMatchWidth->setChecked(false);
    ui->actionMatchHeight->setChecked(false);
    m_engine.setSizeMode(Engine::CropImage);
}

void PrintWindow::on_actionShrinkImages_triggered()
{
    ui->actionCropImages->setChecked(false);
    ui->actionShrinkImages->setChecked(true);
    ui->actionMatchWidth->setChecked(false);
    ui->actionMatchHeight->setChecked(false);
    m_engine.setSizeMode(Engine::ShrinkImage);
}

void PrintWindow::on_actionMatchWidth_triggered()
{
    ui->actionCropImages->setChecked(false);
    ui->actionShrinkImages->setChecked(false);
    ui->actionMatchWidth->setChecked(true);
    ui->actionMatchHeight->setChecked(false);
    m_engine.setSizeMode(Engine::MatchWidth);
}

void PrintWindow::on_actionMatchHeight_triggered()
{
    ui->actionCropImages->setChecked(false);
    ui->actionShrinkImages->setChecked(false);
    ui->actionMatchWidth->setChecked(false);
    ui->actionMatchHeight->setChecked(true);
    m_engine.setSizeMode(Engine::MatchHeight);
}

void PrintWindow::on_actionPrintMarkers_triggered(bool checked)
{
    m_engine.setPrintMarkers(checked);
}

void PrintWindow::on_actionPrint_triggered()
{
    m_preview->print();
}

void PrintWindow::on_width_editingFinished()
{
    double d = getValue(ui->width);
    if(d != 0)
        m_engine.setItemWidth(d);
}

void PrintWindow::on_height_editingFinished()
{
    double d = getValue(ui->height);
    if(d != 0)
        m_engine.setItemHeight(d);
}

double PrintWindow::getValue(QLineEdit *e)
{
    double unit = 1;
    QString str = e->text();
    if(str.endsWith("cm"))
        str = str.left(str.length() - 2);
    else if(str.endsWith("mm")) {
        unit = 0.1;
        str = str.left(str.length() - 2);
    }
    else {
        e->setBackgroundRole(QPalette::Highlight);
        return 0;
    }
    bool ok;
    double val = str.toDouble(&ok);
    if(!ok) {
        e->setBackgroundRole(QPalette::Highlight);
        return 0;
    }
    e->setBackgroundRole(QPalette::Background);
    return val * unit;
}

void PrintWindow::on_printer_activated(int index)
{
    QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
    foreach(QPrinterInfo p, printers) {
        if(p.printerName() == ui->printer->itemText(index)) {
            //selectPrinter(p);
        }
    }
}
